package com.tianyu.inspection.service;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.service.api.IDangerTypeService;
import com.tianyu.inspection.entity.DangerType;
import com.tianyu.inspection.entity.Project;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.DangerTypeMapper;
import com.tianyu.inspection.mapper.ProjectMapper;
import io.ocs.model.OcsSimpleModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 险情类型服务
 * @Date 2021/1/12 - 14:22
 **********************************/

@Service
public class DangerTypeServiceImpl implements IDangerTypeService {

    @Resource
    private DangerTypeMapper dangerTypeMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return ModelMap
     */
    @Override
    public IPage<DangerType> searchListPage(QueryMo queryMo) {
        IPage<DangerType> dangerTypeList = PageModel.getModelPage(queryMo, dangerTypeMapper);
        dangerTypeList.getRecords().forEach(dangerType -> {
            if (Objects.nonNull(dangerType.getIcon())){
                dangerType.setIcon(ocsSimpleModel.getObjectUrl(dangerType.getIcon()));
            }
            /*if (Objects.nonNull(dangerType.getLevelIcon())){
                dangerType.setLevelIcon(appConstants.ossDomain + dangerType.getLevelIcon());
            }
            if (Objects.nonNull(dangerType.getLevelIconClick())){
                dangerType.setLevelIconClick(appConstants.ossDomain + dangerType.getLevelIconClick());
            }*/
        });
        return dangerTypeList;
    }


    @Override
    public List<DangerType> searchList(QueryMo queryMo, Integer projectId) {
        queryMo.setPageSize(-1);
        Project project = projectMapper.selectById(projectId);
        List<DangerType> returnTypeList = new ArrayList<>();
        if (Objects.nonNull(project) && Objects.nonNull(project.getDangerTypeUse())){
            JSONArray jsonArray = JSONArray.parseArray(project.getDangerTypeUse());
            List<DangerType> typeList = searchListPage(queryMo).getRecords();
            typeList.forEach(dangerType -> {
                if (jsonArray.contains(dangerType.getId())){
                    returnTypeList.add(dangerType);
                }
            });
        }
        return returnTypeList;
    }
    /**
     * 通过ID等参数查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 单条记录结果
     */
    @Override
    public DangerType selectOne(QueryMo queryMo) {
        if (Objects.nonNull(queryMo.getEqMap().get("id"))){
            return dangerTypeMapper.selectOne(new QueryWrapper<DangerType>().allEq(queryMo.getEqMap()));
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        DangerType dangerType = objectMapper.convertValue(operateMo.getEntity(), DangerType.class);
        return 1 == dangerTypeMapper.insert(dangerType);
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.getEqMap().get("id"))){
            return 1 == dangerTypeMapper.delete(new QueryWrapper<DangerType>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 通过ID更新一条数据
     *
     * @param operateMo 实体对象参数集
     * @return 是否操作成功
     */
    @Override
    public boolean update(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        DangerType dangerType = objectMapper.convertValue(operateMo.getEntity(), DangerType.class);
        operateMo.getEqMap().remove("project_id");
        return 1 == dangerTypeMapper.update(dangerType,new QueryWrapper<DangerType>().allEq(operateMo.getEqMap()));
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态]
     * @return 是否操作成功
     */
    @Override
    public boolean logicallyDelete(QueryMo queryMo) {
        return 1 == dangerTypeMapper.update(
                new DangerType().setState(1),
                new QueryWrapper<DangerType>().allEq(queryMo.getEqMap())
        );
    }
}
